<template>
  <div class="goods_list_div">
    <!-- 商品列表页面 -->
    <!-- 头部组件 -->
    <my-header title="商品列表" :left="true">
      <!-- 插入 右侧icon -->
      <template class="right_icon" #right_icon>
        <van-popover  v-model="showPopover" theme="dark" trigger="click" placement="bottom-end" >
          <!-- 第一行 首页 -->
          <van-row @click="showPopover=false;$router.push('/home');">
            <!-- 图标部分 -->
            <van-col sapn="4" offset="4">
              <span class="iconfont icon-shouye"></span>
            </van-col>
            <!-- 文字部分 -->
            <van-col sapn="15" offset="1">
              <span class="icon_font">首页</span>
            </van-col>
          </van-row>
          <!-- 第二行 消息-->
           <van-row  @click="showPopover=false;$router.push('/message');">
            <!-- 图标部分 -->
            <van-col sapn="4" offset="4">
              <span class="iconfont icon-youjian"></span>
            </van-col>
            <!-- 文字部分 -->
            <van-col sapn="15" offset="1">
              <span class="icon_font">消息</span>
            </van-col>
          </van-row>
          <template #reference>
            <span class="iconfont icon-gengduo"></span>
          </template>
        </van-popover>
      </template>
    </my-header>
    <!-- 二级排序 -->
    <chooes-sort class="chooes_sort" @search_sort="search_sort" :search_condition="search_condition"></chooes-sort>
    <!-- 皮肤展示区域 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        error-text="请求失败，点击重新加载"
        :error.sync="error"
      >
      <!-- 内容展示区域 -->
      <div v-if="skin_data.gameid">
      <show-in-fo 
        v-for="(item,index) of skin_data.gameid" 
        :key="item.gameid"
        :game_data_index="index" 
        :game_data="skin_data"></show-in-fo>
      </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
// 头部组件
import MyHeader from "../common/Header";
// 二级排序组件
import ChooesSort from "./components/ChooesSort";
// 导入皮肤展示组件
import ShowInFo from "../../views/Home/components/ShowInFo";
import isObjectValueEqual from "../../assets/js/lib/isObj";
export default {
  name:"GoodsList",
  created(){
    Object.assign(this.search_condition,this.$route.query);
    this.onLoad();
  },
  data() {
    return {
      showPopover: false,
      // 存储搜索条件
      search_condition:{
        ip:"",// IP地址
        search:"",// 排序方式
        type:"",// 操作系统
        minPrice:"",// 最小价格
        maxPrice:"",// 最大价格
        roleCount:"",// 角色数量
        xsCount:"",// 时装数量
        xsSui:"",// 随从数量
        searchid:"",// 包含的皮肤ID
        isok:"",// 价格升序or 降序
        tiao_index:"",// 跳几条
      },
      skin_data:{// 请求的皮肤数据
        gameid:[],
        jin_count:[],
        role_count:[],
        skinList:[],
        zi_count:[],
      },
      totleCount:0,// 总条数
      error: false,
      loading: false,
      finished: false,
      refreshing: false,
    }
  },
  methods: {
    // ...mapActions("Ip",{
    //   setIp:"setIp"
    // }),
    // 排序方式赋值
    search_sort(val){
      if(val.type == "price"){// 如果当前筛选价格
        if(val.slot){
          this.search_condition.isok = "true";
        }else{
          this.search_condition.isok = "false";
        }
      }else{
        this.search_condition.isok = "";
      }
      this.search_condition.search = val.type;
      this.onRefresh();
    },
    // 请求皮肤数据
    async get_skin_data(){
      // 先请求IP地址
      // const ip = await this.getip();
      let ip = "";
      this.search_condition.ip = ip;
      const {data:res} = await this.$http.get("getSkinList",{params:this.search_condition}).catch(() => {
        this.error = true;
        this.loading = false;
      });
      this.$message({position:"center",message:res.message});
      if(this.refreshing){
        // 重新加载数据
        this.search_condition.tiao_index = 0 + "";
        this.skin_data.gameid = [];
        this.skin_data.jin_count = [];
        this.skin_data.role_count = [];
        this.skin_data.skinList = [];
        this.skin_data.zi_count = [];
        this.refreshing = false;
      }
      if(!res.isok){
        this.loading = false;
        this.finished = true;
        return;
      }
      for (let index = 0; index < res.data.gameid.length; index++) {
        this.skin_data.gameid.push(res.data.gameid[index]);
        this.skin_data.jin_count.push(res.data.jin_count[index]);
        this.skin_data.role_count.push(res.data.role_count[index]);
        this.skin_data.skinList.push(res.data.skinList[index]);
        this.skin_data.zi_count.push(res.data.zi_count[index]);
      }
      this.totleCount = res.data.totleCount;
      console.log(res);
    },
    // 请求IP地址
    async getip(){
      // 去 vuex 里 查询 是否有IP 如果有 则 不请求 如果没有则请求
      // if(this.getIp) return this.getIp;
      // console.log("请求了IP地址");
      // let url = "http://ip-api.com/json";
      // const data = await jsonp(url);
      // // 赋值给vuex
      // this.setIp(data.query);
      // return data.query;
    },
    async onLoad() {
      // 发起网络请求
      // console.log("发起网络请求");
      this.loading = true;
      await this.get_skin_data();
      this.loading = false;
      let tiao_index = this.search_condition.tiao_index - 0;
      tiao_index += 10;
      this.search_condition.tiao_index = tiao_index + "";
      if(this.skin_data.gameid.length >= this.totleCount){
        this.finished = true;
      }
    },
    // 重新加载数据
    async onRefresh() {
      // console.log("onRefresh");
      this.search_condition.tiao_index = '0';
      this.refreshing = true;// 请空数据
      this.finished = false;
      this.onLoad();
    },
  },
  components:{
    MyHeader,
    ChooesSort,
    ShowInFo
  },
  computed: {
  },
  watch: {
    '$route':function(val){
      console.log(val);
      if(val.path != "/goodslist"){// 如果访问的不是本页面 则不查询
        return;
      }
      let obg = {
        ip:"",// IP地址
        search:"",// 排序方式
        type:"",// 操作系统
        minPrice:"",// 最小价格
        maxPrice:"",// 最大价格
        roleCount:"",// 角色数量
        xsCount:"",// 时装数量
        xsSui:"",// 随从数量
        searchid:"",// 包含的皮肤ID
        isok:"",// 价格升序or 降序
        tiao_index:"",// 跳几条
      }
      Object.assign(obg,val.query);
      if(isObjectValueEqual(obg,this.search_condition)){
        console.log("参数相同 本次不请求数据");
        return;
      }
      this.search_condition = obg;
      console.log(obg);
      console.log(this.search_condition);
      this.onRefresh();
    }
  },
}
</script>
<style lang="less" scoped>
  .goods_list_div{
    background-color: #fff;
    padding-top: 0.95rem;
  }
  .icon-gengduo{
    font-size: 0.18rem;
    font-weight: bold;
  }
  .van-popover{
    .van-row{
      width: 1rem;
      font-size: 0.15rem;
      height: 0.38rem;
      text-align: center;
      border-top: 0.01rem solid #3F3E48;
      line-height: 0.35rem;
    }
  }
  .chooes_sort{
    z-index: 999;
  }
</style>